package gold.enterprise;

public class TencentPCGTwo {


    public static String numMinus(String num1, String num2) {
        if (null == num1 && null == num2) return null;
        if (null == num1) return "-" + num2;
        if (null == num2) return num1;

        if (num1.length() < num2.length()) return "-" + numMinus(num2, num1);
        if (num1.length() == num2.length() && num1.compareTo(num2) <= 0) return "-" + numMinus(num2, num1);

        //num length >= num2.length
        StringBuilder res = new StringBuilder();

        int borrow = 0, i = num2.length() - 1, j = num1.length() - 1;
        for (; i >= 0; --i, --j) {
            int c2 = num2.charAt(i) - '0';
            int c1 = num1.charAt(j) - '0';

            if (c1 - c2 - borrow >= 0) {
                res.insert(0, String.valueOf(c1 - c2 - borrow));
                borrow = 0;
            } else {
                res.insert(0, String.valueOf(10 + c1 - c2 - borrow));
                borrow = 1;
            }
        }

        for (; j >= 0; j--) {
            int c1 = num1.charAt(j) - '0' - borrow;
            if (c1 >= 0) {
                res.insert(0, String.valueOf(c1));
                borrow = 0;
            } else {
                res.insert(0, String.valueOf(10 + c1));
                borrow = 1;
            }
        }

        while (res.charAt(0) == '0') res.delete(0, 1);
        return res.toString();
    }


    public static void main(String[] args) {
        System.out.println(numMinus("1", "1000"));

    }

}


